﻿using Furion.FriendlyException;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using WeChatV3Pay.Core;
using WeChatV3Pay.Core.Dtos;
using WeChatV3Pay.Core.Enums;

namespace WeChatV3Pay.Application.Transactions.Dtos.JsApi
{
    /// <summary>
    /// JsApi下单模型
    /// </summary>
    public class JsApiPayReq : JsApiPayDto, IValidatableObject
    {
        /// <summary>
        /// 证书私钥
        /// </summary>
        public string PrivateKey { get; set; }

        /// <summary>
        /// 证书编号
        /// </summary>
        public string SerialNo { get; set; }

        public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
        {
            if (string.IsNullOrWhiteSpace(PrivateKey))
            {
                throw Oops.Oh(ErrorCodes.PRIVATE_KEY_IS_NULL);
            }
            if (string.IsNullOrWhiteSpace(SerialNo))
            {
                throw Oops.Oh(ErrorCodes.SERIAL_NO_IS_NULL);
            }
            if (string.IsNullOrWhiteSpace(AppId))
            {
                throw Oops.Oh(ErrorCodes.transactions_appid_is_null);
            }
            if (string.IsNullOrWhiteSpace(MchId))
            {
                throw Oops.Oh(ErrorCodes.transactions_mchid_is_null);
            }
            if (string.IsNullOrWhiteSpace(Description))
            {
                throw Oops.Oh(ErrorCodes.transactions_description_is_null);
            }
            if (string.IsNullOrWhiteSpace(OutTradeNo))
            {
                throw Oops.Oh(ErrorCodes.transactions_out_trade_no_is_null);
            }
            if (string.IsNullOrWhiteSpace(NotifyUrl))
            {
                throw Oops.Oh(ErrorCodes.transactions_notify_url_is_null);
            }
            if (Amount == null)
            {
                throw Oops.Oh(ErrorCodes.transactions_amount_is_null);
            }
            if (Amount.Total <= 0)
            {
                throw Oops.Oh(ErrorCodes.transactions_amount_total_less_than_0);
            }
            if (string.IsNullOrWhiteSpace(Amount.Currency))
            {
                throw Oops.Oh(ErrorCodes.transactions_amount_currency_is_null);
            }
            if (Payer == null)
            {
                throw Oops.Oh(ErrorCodes.transactions_payer_is_null);
            }
            if (string.IsNullOrWhiteSpace(Payer.OpenId))
            {
                throw Oops.Oh(ErrorCodes.transactions_payer_openid_is_null);
            }
            yield return new ValidationResult("");
        }
    }
}
